package repository.impl;

import entity.Log;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import repository.LogRepository;

import java.util.List;

@Transactional
@Repository
public class LogRepositoryImpl implements LogRepository {

    @Autowired
    private SessionFactory sessionFactory;

    private Session getCurrentSession() {
        return this.sessionFactory.getCurrentSession();
    }

    public Log load(Integer id) {
        return (Log) getCurrentSession().load(Log.class, id);
    }

    public Log get(Integer id) {
        return (Log) getCurrentSession().get(Log.class, id);
    }

    public List<Log> findAll() {
        Query query = getCurrentSession().createQuery("from " + Log.class.getName());
        return query.list();
    }

    public void persist(Log entity) {

    }

    public Integer save(Log entity) {
        return (Integer) getCurrentSession().save(entity);
    }

    public void saveOrUpdate(Log entity) {
        getCurrentSession().saveOrUpdate(entity);
    }

    public void delete(Integer id) {
        Log log = load(id);
        getCurrentSession().delete(log);
    }

    public void flush() {

    }
}
